import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //查询全部
        m1();
        // 模糊匹配
        m2();
        //增加
        m3();
        //删除
        m4();
    }

    private static void m1() throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/jdbc";
        Connection connection = DriverManager.getConnection(url, "root", "Xlz922?*");
        if (connection != null) {
            System.out.println("连接成功");
        }

        //3.写sql
        String sql = "select * from user";
        //4.预编译sql
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //5.有参数设置参数
        //6.执行sql
        ResultSet resultSet = preparedStatement.executeQuery();
        //7.遍历结果集
        while (resultSet.next()) {//获取
            int id = resultSet.getInt(1);//Int用数字是列数，String用列名
            String name = resultSet.getString(2);
            String pass = resultSet.getString(3);
            System.out.println(id + "-------" + name + "-------" + pass);

        }
        //8.关闭资源

        resultSet.close();
        preparedStatement.close();
        connection.close();

    }


    private static void m2() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入你要查询的姓名");
            String word = sc.next();
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取连接
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc";
            connection = DriverManager.getConnection(url, "root", "112816");
//            if(connection !=null){
//                System.out.println("连接成功");
//            }
            //3.写sql
            String sql = "select * from user where name like ?";
            //4.预编译sql
            preparedStatement = connection.prepareStatement(sql);

            //5.有参数设置参数
            preparedStatement.setObject(1, "%" + word + "%");//设置参数

            //6.执行sql
            resultSet = preparedStatement.executeQuery();

            //7.遍历结果集
            while (resultSet.next()) {//获取
                int id = resultSet.getInt(1);//Int用数字是列数，String用列名
                String name = resultSet.getString(2);
                String pass = resultSet.getString(3);
                System.out.println(id + "-------" + name + "-------" + pass);

            }
        }
        //8.管资源


        catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (resultSet != null)
                    resultSet.close();
                if (preparedStatement != null)
                    preparedStatement.close();
                if (connection != null)
                    connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
    }


    private static void m3() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要增加的id");
            int id = sc.nextInt();
            System.out.println("请输入要新增的姓名");
            String name = sc.next();
            System.out.println("请输入要新增的密码");
            String pass = sc.next();
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取连接
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?severTimezone=GMT%2B8&useSSL=false";
            connection = DriverManager.getConnection(url, "root", "112816");
//            if(connection !=null){
//                System.out.println("连接成功");
//            }
            //3.写sql
            String sql = "insert into user(id,name,pass) values (?,?,?)";
            //4.预编译sql
            preparedStatement = connection.prepareStatement(sql);

            //5.有参数设置参数
            preparedStatement.setObject(1, id);
            preparedStatement.setObject(2, name);//设置参数
            preparedStatement.setObject(3, pass);

            //6.执行sql
            int result = preparedStatement.executeUpdate();

            //7.遍历结果集
            if (result > 0) {
                System.out.println("新增成功");
            }
            //8.关闭资源
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (preparedStatement != null)
                    preparedStatement.close();
                if (connection != null)
                    connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }

    private static void m4() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要删除");
            int id = sc.nextInt();
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取连接
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc";
            connection = DriverManager.getConnection(url, "root", "112816");
//            if(connection !=null){
//                System.out.println("连接成功");
//            }
            //3.写sql
            String sql = "delete from user where id=?";
            //4.预编译sql
            preparedStatement = connection.prepareStatement(sql);

            //5.有参数设置参数
            preparedStatement.setObject(1, id);//设置参数

            //6.执行sql
            int result = preparedStatement.executeUpdate();

            //7.遍历结果集

            if (result > 0) {
                System.out.println("新增成功");
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (resultSet != null)
                    resultSet.close();
                if (preparedStatement != null)
                    preparedStatement.close();
                if (connection != null)
                    connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }
}